﻿using QunProject.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QunProject.Manage.Controllers
{
    public class ReplyController : BaseController
    {
        // 首页
        public ActionResult Index()
        {
            return View();
        }
        // 获取数据
        public JsonResult AjaxData(int? articleId, int page = 1, int limit = 20)
        {
            var list = EF.reply.Where(x => true);
            if (articleId.HasValue)
                list = list.Where(x => x.ArticleID == articleId);

            var data = list.OrderByDescending(x => x.CreateTime).ToList().Skip((page - 1) * limit).Take(limit).Select(x => new
            {
                x.ID,
                CreateTime = x.CreateTime.Value.ToString("yyyy-MM-dd hh:mm:ss"),
                x.Remark,
                x.ParentID,
                x.MemberID,
                x.Content,
                x.Praise,
                x.IsDisplay,
                IsDisplayName = x.IsDisplay.Value ? "显示" : "隐藏",
                x.Criticism,
                x.ArticleID
            });

            return Json(Result.SuccessReturnData(list.Count(), data));
        }
        // 提交隐藏
        public JsonResult AjaxDisplay(string ids)
        {
            var delIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (delIds.Length == 0)
                return Json(Result.Fail("请选择需要改变的内容"));

            foreach (var id in delIds)
            {
                var modelId = id.ToInt();
                var model = EF.reply.SingleOrDefault(x => x.ID == modelId);
                model.IsDisplay = !model.IsDisplay;
            }

            bool isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
    }
}